package com.qdu.hyy.MovieGenreCount;

import com.qdu.hyy.MovieWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

//JOB1   已完成

public class MovieGenreCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {

    private IntWritable movieWritable = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] fields = value.toString().split(","); // 使用逗号作为分隔符

//        System.out.println("Input line: " + line);
//        for (int i = 0; i < fields.length; i++) {
//            System.out.println("Field " + i + ": " + fields[i]);
//        }

        if(fields.length < 22){
            return;
        }
        if (fields.length >= 5) {



            String genres = fields[4];
            String[] genresArray = genres.split("\\|");

            for (String genre : genresArray) {
                // 去除前后空格，并转换为Text对象
                if(genre.equals(null))continue;
                Text genreKey = new Text(genre.trim());

                // 输出键值对
                context.write(genreKey, movieWritable);
            }




            }

    }
}
